package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.bean.AddressBook;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hebing
 * @version 1.0
 */
@Service
@Transactional
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 添加
     *
     * @param addressBook
     */
    @Override
    public void save(AddressBook addressBook) {
        //添加
        addressBookMapper.insert(addressBook);
    }

    /**
     * 根据条件修改默认地址
     *
     * @param wrapper
     */
    @Override
    public void update(LambdaUpdateWrapper<AddressBook> wrapper) {
        //修改
        addressBookMapper.update(null,wrapper);
    }

    /**
     * 根据id修改默认地址
     *
     * @param addressBook
     */
    @Override
    public void updateById(AddressBook addressBook) {
        addressBookMapper.updateById(addressBook);
    }

    /**
     * 根据id查询记录
     *
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        //查询
        AddressBook addressBook = addressBookMapper.selectById(id);
        return addressBook;
    }

    /**
     * 根据用户id和默认地址进行查询
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public AddressBook getOne(LambdaQueryWrapper<AddressBook> queryWrapper) {
        //根据条件进行查询
        AddressBook addressBook = addressBookMapper.selectOne(queryWrapper);
        return addressBook;
    }

    /**
     * 根据用户id查询地址簿
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public List<AddressBook> list(LambdaQueryWrapper<AddressBook> queryWrapper) {
        //根据用户id查询得到AddressBook地址簿集合
        List<AddressBook> addressBookList = addressBookMapper.selectList(queryWrapper);
        //返回
        return addressBookList;
    }
}
